package tr.edu.metu.is.mega.client.course.instructor.students;

import tr.edu.metu.is.mega.client.core.DialogCallbackAdapter;
import tr.edu.metu.is.mega.client.resource.MEGAIconBundle;
import tr.edu.metu.is.mega.client.resource.MEGAResourceBundle;
import tr.edu.metu.is.mega.client.resource.MEGAResources;
import tr.edu.metu.is.mega.client.widgets.MegaCompositeWidget;
import tr.edu.metu.is.mega.common.dto.CourseDTO;
import tr.edu.metu.is.mega.common.dto.TeamDTO;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.HBoxLayout;
import com.extjs.gxt.ui.client.widget.layout.HBoxLayout.HBoxLayoutAlign;
import com.extjs.gxt.ui.client.widget.layout.HBoxLayoutData;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.AbstractImagePrototype;

/**
 * Team selection widget.
 * 
 */
public class TeamWidget extends LayoutContainer implements MegaCompositeWidget {

    private TeamDTO selectedTeam;

    private TextField<String> txtTeamDesc;

    private final MEGAIconBundle iconBundle = MEGAResources.ICONBUNDLE;

    private final MEGAResourceBundle resourceBundle = MEGAResources.RESOURCEBUNDLE;

    private final TeamSelectDialog userSelectDialog;

    /**
     * Default constructor.
     */
    public TeamWidget(CourseDTO course) {
        userSelectDialog = new TeamSelectDialog(course);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void onRender(Element parent, int index) {
        super.onRender(parent, index);

        HBoxLayout hBoxLayout = new HBoxLayout();
        hBoxLayout.setHBoxLayoutAlign(HBoxLayoutAlign.TOP);
        setLayout(hBoxLayout);

        txtTeamDesc = new TextField<String>();
        txtTeamDesc.setReadOnly(true);

        Button buttonSelect = new Button();
        buttonSelect.setIcon(AbstractImagePrototype.create(iconBundle.search()));
        buttonSelect.setToolTip(resourceBundle.select());
        buttonSelect.addSelectionListener(new SelectionListener<ButtonEvent>() {

            @Override
            public void componentSelected(ButtonEvent ce) {
                userSelectDialog.select(new DialogCallbackAdapter<TeamDTO>() {

                    @Override
                    public void onOk(TeamDTO param) {
                        setValue(param);
                    }

                });
            }
        });

        Button buttonClear = new Button();
        buttonClear.setIcon(AbstractImagePrototype.create(iconBundle.clear()));
        buttonClear.setToolTip(resourceBundle.delete());
        buttonClear.addSelectionListener(new SelectionListener<ButtonEvent>() {

            @Override
            public void componentSelected(ButtonEvent ce) {
                setValue(null);
            }
        });

        add(txtTeamDesc, new HBoxLayoutData(new Margins(0, 2, 0, 0)));
        add(buttonSelect, new HBoxLayoutData(new Margins(0, 2, 0, 0)));
        add(buttonClear, new HBoxLayoutData(new Margins(0)));
    }

    /**
     * Sets selected person.
     * 
     * @param selectedUser the person to set
     */
    public void setValue(TeamDTO selectedUser) {
        selectedTeam = selectedUser;
        if (selectedUser == null) {
            txtTeamDesc.setValue(null);
        } else {
            txtTeamDesc.setValue(selectedUser.getTeamName());
        }
    }

    /**
     * @return selected team
     */
    public TeamDTO getValue() {
        return selectedTeam;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Object getWidgetValue() {
        return getValue();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setWidgetValue(Object value) {
        if (!(value instanceof TeamDTO)) {
            throw new IllegalArgumentException("Parameter is not TeamDTO");
        }
        setValue((TeamDTO) value);
    }

}
